package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.atguigu.daijia.common.constant.RedisConstant.USER_LOGIN_KEY_PREFIX;
import static com.atguigu.daijia.common.constant.RedisConstant.USER_LOGIN_KEY_TIMEOUT;

/**
 * @author moningxi
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 小程序授权登录
     */
    @Override
    public String login(String code) {
        Result<Long> result = customerInfoFeignClient.login(code);
        // 错误判断
        Long uid = result.getData();
        if (!ResultCodeEnum.SUCCESS.getCode().equals(result.getCode()) ||
                uid == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        // 生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(USER_LOGIN_KEY_PREFIX + token, uid.toString(), USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);

        return token;
    }

    /**
     * 获取用户信息
     */
    @Override
    public CustomerLoginVo getCustomerInfo(String token) {
        String uid = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if (!StringUtils.hasText(uid)) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerInfo(Long.valueOf(uid));
        CustomerLoginVo customerInfo = result.getData();
        if (!ResultCodeEnum.SUCCESS.getCode().equals(result.getCode())
                || customerInfo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerInfo;
    }

    /**
     * 获取用户信息
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(Long userId) {
        Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerInfo(userId);
        CustomerLoginVo customerInfo = result.getData();
        if (!ResultCodeEnum.SUCCESS.getCode().equals(result.getCode())
                || customerInfo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerInfo;
    }

    /**
     * 更新用户手机号
     */
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }
}
